#include "wholeprogress.h"

#include <qdebug.h>

// TODO：游戏开始时进行计时，所有对局结束后返回战斗时间进行结算
//       根据杀敌数和战斗时间进行给分。
// 排行榜显示 数据库建好了
// 不同的角色选用不同的战斗模组，wholeprogress 进行改动
// 战斗结束生成游戏统计图

/*
 * @Brief:  战斗初始化，生成玩家和不同的敌人
 * @Param:  playerType--玩家的职业
 * @Param:  diffi--选择的难度
 * @Return: NULL
 */
const int EX = 1045, EY = 195, PX = 130, PY = 150;
WholeProgress::WholeProgress(int playerType, int diffi) {
  playername = new player(playerType);
  this->playerT = playerType;

  // 将所有敌人存入vector
  for (int i = 0; i < diffi * 2 + 2; i++) {
    enemy anEnemy;
    anEnemy.setHp(40 + 5 * diffi);
    anEnemy.setMaxHp(40 + 5 * diffi);  // 怪物血量伤害的设置
    anEnemy.setAtack(i + 1);
    wholeEnemy.push_back(anEnemy);
  }
}

/*
 * @Brief:  每次进行战斗时执行一次，初始化双方手牌和对象
 * @Return: vector<card> 玩家手牌
 */
vector<card> WholeProgress::battleStart() {
  playername->clearbuff();
  playername->manager->CardInYourHand.clear();
  playername->setMp(0 - playername->getMp());
  playername->setMp(playername->getMaxMp());

  if (!wholeEnemy.empty()) {
    myEnemy = wholeEnemy.front();
    vector<enemy>::iterator k = wholeEnemy.begin();

    wholeEnemy.erase(k);
  }
  myEnemy.clearbuff();

  playername->manager->Card2DrugList = playername->manager->CardCanUse;
  qDebug() << "cardcanuse" << playername->manager->CardCanUse.size();

  // 打乱牌组顺序
  srand(unsigned(time(NULL)));
  random_shuffle(playername->manager->Card2DrugList.begin(),
                 playername->manager->Card2DrugList.end());

  // cardNumber 角色一回合可抽几张牌
  playername->manager->wastedCard.clear();
  for (int i = 0; i < playername->cardNumber; i++) {
    vector<card>::iterator k = playername->manager->Card2DrugList.begin();
    playername->manager->Card2DrugList.erase(k);
    playername->manager->CardInYourHand.push_back(*k);
  }
  return playername->manager->CardInYourHand;
}

/*
 * @Brief:  判断玩家是否可以打出这张牌
 * @Param:  choice--玩家选择的牌的编号
 * @Param:  type--玩家职业
 * @Return: bool--是否使用成功
 */
bool WholeProgress::ifuse(int choice, int type) {
  card useCard = playername->manager->CardInYourHand[choice];  // 这次用的牌

  if (type == 1) {
    /* if ((useCard.getCardType() == 1) &&
         (playername->getHp() < useCard.getMp())) {
       return false;
     } else */

    if (playername->getMp() < useCard.getMp()) {
      return false;
    }
    return true;
  }

  if (type == 2) {
    if ((useCard.getCardType() == 3) &&
        (playername->manager->CardInYourHand.size() == 1)) {
      return true;
    } else if (playername->getMp() >= useCard.getMp()) {
      return true;
    }
    return false;
  }

  return false;
}

/*
 * @Brief:  返回当前对象出牌后自身和对方属性变化
 * @Param:  choice--打出的牌
 * @Return: mydata结构体
 */
mydata WholeProgress::dataReturn(int choice) {
  mydata curData;

  qDebug() << "成功进入";

  if (choice != 100) {  // 表示玩家结束回合
    card useCard = playername->manager->CardInYourHand[choice];  // 这次用的牌
    vector<card>::iterator k =
        playername->manager->CardInYourHand.begin() + choice;
    qDebug() << "删除" << k->getDescribe();
    if (!useCard.consume) {
      playername->manager->wastedCard.push_back(*k);
    }
    playername->manager->CardInYourHand.erase(k);
    curData = dataprocess(curData, useCard);
  }

  return curData;
}

mydata WholeProgress::dataprocess(mydata curdata, card useCard) {
  int flag = 1;

  if (playerT == 1) {
    qDebug() << "玩家一" << useCard.getCardType() << useCard.getPicPath();

    switch (useCard.getCardType()) {
      case 0:  // 打击
        myEnemy.setHp((0 - useCard.getAttack() - playername->getExtraPower() -
                       myEnemy.getbuff(4)));  // 敌方扣血
        emit showHurtNumber(
            QString::number(0 - useCard.getAttack() -
                            playername->getExtraPower() - myEnemy.getbuff(4)),
            EX, EY, 200, QColorConstants::Red);

        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值

        if (myEnemy.getHp() == 0) {  // 敌方死亡
          flag = 0;
        }
        break;

      case 1:  // 防御
        playername->setShield(useCard.getAttack() + playername->getbuff(2));
        emit showHurtNumber("+" + QString::number(useCard.getAttack()), 130,
                            150, 0, QColorConstants::Gray);  // 显示护盾数值
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        break;
        break;

      case 2:  // 放血

        playername->setMp((0 - useCard.getMp()));

        // 我方消耗法力值
        playername->setMp(3);      // 卡牌恢复法力值
        playername->setHp(0 - 2);  // 卡牌消耗生命

        if (myEnemy.getHp() == 0) {
          // 敌方死亡
          flag = 0;
        }

        break;

      case 3:                                      // 御血术
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        myEnemy.setHp((0 - useCard.getAttack() - playername->getExtraPower() -
                       myEnemy.getbuff(4)));  // 敌方扣血
        emit showHurtNumber(
            QString::number(0 - useCard.getAttack() -
                            playername->getExtraPower() - myEnemy.getbuff(4)),
            EX, EY, 200, QColorConstants::Red);
        if (myEnemy.getHp() == 0) {
          // 敌方死亡
          flag = 0;
        }

        playername->setHp(0 - 2);  // 卡牌消耗生命
        break;

      case 4:                                        // 岿然不动
        playername->setMp((0 - useCard.getMp()));    // 我方消耗法力值
        playername->setShield(useCard.getAttack());  // 护盾
        break;

      case 5:                                      // 暴走
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        myEnemy.setHp((0 - useCard.getAttack() - playername->getbuff(1) -
                       8 * playername->getbuff(5) -
                       myEnemy.getbuff(4)));  // 敌方扣血
        emit showHurtNumber(
            QString::number(0 - useCard.getAttack() -
                            playername->getExtraPower() - myEnemy.getbuff(4) -
                            8 * playername->getbuff(5)),
            EX, EY, 200, QColorConstants::Red);
        playername->addbuff(5, 1);  // 暴走层数+1
        break;

      case 6:  // 重锤
               //  playername->setShield(playername->getShield() * 2);
               //  emit showHurtNumber("+" +
               //  QString::number(playername->getShield() * 2),
               //                      130, 150, 0,
               //                      QColorConstants::Gray);  // 显示护盾数值
        myEnemy.setHp((0 - useCard.getAttack() - playername->getExtraPower() -
                       myEnemy.getbuff(4)));  // 敌方扣血
        emit showHurtNumber(
            QString::number(0 - useCard.getAttack() -
                            playername->getExtraPower() - myEnemy.getbuff(4)),
            EX, EY, 200, QColorConstants::Red);
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        break;

      case 7:  // 中和
        myEnemy.setHp((0 - useCard.getAttack() - playername->getExtraPower() -
                       myEnemy.getbuff(4)));  // 敌方扣血
        emit showHurtNumber(
            QString::number(0 - useCard.getAttack() -
                            playername->getExtraPower() - myEnemy.getbuff(4)),
            EX, EY, 200, QColorConstants::Red);
        playername->setMp((0 - useCard.getMp()));  //

        // 我方消耗法力值
        myEnemy.addbuff(3, 1);
        // emit showHurtNumber(
        //     QString::number(-useCard.getAttack() -
        //     playername->getExtraPower()), EX, EY, 200, QColorConstants::Red);

        // 敌方扣血

        // playername->setExtraPower(useCard.getBuff());

        if (myEnemy.getHp() == 0) {  //

          // 敌方死亡
          flag = 0;
        }
        break;

      case 8:  // 痛击
        myEnemy.setHp((0 - useCard.getAttack() - playername->getExtraPower() -
                       myEnemy.getbuff(4)));  // 敌方扣血
        emit showHurtNumber(
            QString::number(0 - useCard.getAttack() -
                            playername->getExtraPower() - myEnemy.getbuff(4)),
            EX, EY, 200, QColorConstants::Red);
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        myEnemy.addbuff(4, 1);
        if (myEnemy.getHp() == 0) {  //

          // 敌方死亡
          flag = 0;
        }
        break;
      case 9:                                      // 子弹时间
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        playername->setMp((0 + 999));              // 我方消耗法力值
        playername->addbuff(6, 1);                 // 死亡层数+1
        break;
      case 10:                                     // 残杀
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        myEnemy.setHp((0 - useCard.getAttack() - playername->getExtraPower() -
                       myEnemy.getbuff(4)));  // 敌方扣血
        emit showHurtNumber(
            QString::number(0 - useCard.getAttack() -
                            playername->getExtraPower() - myEnemy.getbuff(4)),
            EX, EY, 200, QColorConstants::Red);
        if (myEnemy.getHp() == 0) {  //

          // 敌方死亡
          flag = 0;
        }
        break;
      default:
        break;
    }
  } else if (playerT == 2) {
    qDebug() << "玩家二  " << useCard.getCardType() << useCard.getPicPath();

    switch (useCard.getCardType()) {
      case 1:
        myEnemy.setHp((0 - useCard.getAttack() -
                       playername->getExtraPower()));  // 敌方扣血
        emit showHurtNumber(
            QString::number(-useCard.getAttack() - playername->getExtraPower()),
            EX, EY, 200, QColorConstants::Red);
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值

        if (myEnemy.getHp() == 0) {  // 敌方死亡
          flag = 0;
        }
        break;

      case 2:
        playername->getSpeShiheld(useCard.getAttack());  // 持续两回合的护盾！！
        emit showHurtNumber("+" + QString::number(useCard.getAttack()), PX, PY,
                            0, QColorConstants::Gray);
        // 显示护盾
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        break;

      case 3:
        myEnemy.setHp(0 - useCard.getAttack() - playername->getExtraPower());
        emit showHurtNumber(
            QString::number(-useCard.getAttack() - playername->getExtraPower()),
            EX, EY, 200, QColorConstants::Red);

        // 敌方扣血
        playername->setMp((0 - useCard.getMp()));

        // 我方消耗法力值

        if (myEnemy.getHp() == 0) {
          // 敌方死亡
          flag = 0;
        }
        break;

      case 4:
        playername->setHp(0 - useCard.getAttack());
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值

        if (myEnemy.isWeak) {
          updataCardList(1);
        }

        break;

      case 5:

        playername->setHp(0 - useCard.getAttack());
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        myEnemy.isWeak = true;
        break;

      case 6:
        playername->setHp(0 - useCard.getAttack());
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        myEnemy.posionNum += 6;
        break;

      case 7:
        playername->setHp(0 - (10 * playername->getMp()));
        playername->setMp(playername->getMp());  // 我方消耗法力值
        break;

      case 8:

        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        dropCard(1);
        updataCardList(4);

        if (myEnemy.getHp() == 0) {  // 敌方死亡
          flag = 0;
        }
        break;

      case 9:
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        updataCardList(2);
        break;

      case 10:
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        myEnemy.posionNum += 12;
        break;

      case 11:
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        myEnemy.posionNum = myEnemy.posionNum * 2;
        break;

      case 12:
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        myEnemy.posionNum += 6;
        break;

      case 13:
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        dropCard(2);
        updataCardList(2);
        break;

      case 14:
        playername->setMp((0 - useCard.getMp()));  // 我方消耗法力值
        dropCard(1);
        playername->setShield(11);
        break;

      default:
        break;
    }
  } else {
    qDebug() << "为满足其他条件";
  }
  curdata.isAlive = flag;
  curdata.playerHp = playername->getHp();
  curdata.playerMp = playername->getMp();
  curdata.shield = playername->getShield();
  curdata.extraPower = playername->getExtraPower();
  curdata.enemyHp = myEnemy.getHp();
  return curdata;
}

vector<card> WholeProgress::returnCardInHand() {
  if (playername->manager->CardInYourHand.size() != 0)
    return playername->manager->CardInYourHand;
  else {
    card newcard;
    newcard.setMp(1000);
    vector<card> fakevec;
    fakevec.push_back(newcard);
    return fakevec;
  }
}

/*
 * @Brief:  玩家购买卡牌
 * @Param:  choice 所购买牌的id
 * @Return: NULL
 */
void WholeProgress::purchaseProgress(int choice) {
  vector<card>::iterator k = playername->manager->CardGroup.begin();

  // id匹配时，将这张牌添加到canuse中,并从group中删除该牌。
  for (int i = 0; i < playername->manager->AllCardGroup.size(); i++) {
    if (choice == playername->manager->AllCardGroup[i].id) {
      playername->addMoney(0 - playername->manager->AllCardGroup[i].getPrice());
      playername->manager->CardCanUse.push_back(
          playername->manager->AllCardGroup[i]);
      // playername->manager->AllCardGroup.erase(k);
      break;
    }
    k++;
  }
}

// 每次轮到自己时抽牌更新
// 重构
vector<card> WholeProgress::updataCardList(int size) {
  // if (playername->manager->Card2DrugList.empty())
  //   playername->manager->Card2DrugList = playername->manager->CardCanUse;

  // 打乱牌组顺序
  srand(unsigned(time(NULL)));
  random_shuffle(playername->manager->Card2DrugList.begin(),
                 playername->manager->Card2DrugList.end());
  // while(!playername->manager->CardInYourHand.empty())
  // {
  //     playername->manager->wastedCard.push_back(playername->manager->CardInYourHand.back());
  //     playername->manager->CardInYourHand.pop_back();
  // }
  // 弃置上回合全部手牌
  playername->manager->CardInYourHand.clear();
  for (int i = 0; i < size; i++) {
    if ((playername->manager->CardInYourHand.size() >= 7)) break;
    if (playername->manager->Card2DrugList.empty()) {
      playername->manager->Card2DrugList = playername->manager->wastedCard;
      playername->manager->wastedCard.clear();
      srand(unsigned(time(NULL)));
      random_shuffle(playername->manager->Card2DrugList.begin(),
                     playername->manager->Card2DrugList.end());
      // 洗牌
    }
    playername->manager->CardInYourHand.push_back(
        playername->manager->Card2DrugList.back());

    playername->manager->Card2DrugList.pop_back();
  }
  return playername->manager->CardInYourHand;
}

void WholeProgress::dropCard(int size)  // 使用后删除牌
{
  for (int i = 0; i < size; i++) {
    if (playername->manager->CardInYourHand.size() <= 0) {
      break;
    }
    auto k = playername->manager->CardInYourHand.begin();
    playername->manager->wastedCard.push_back(*k);
    playername->manager->CardInYourHand.erase(k);
  }
}

// 从新开始本局游戏
vector<card> WholeProgress::reStartBattle() {
  playername->manager->CardInYourHand.clear();
  playername->setMp(0 - playername->getMp());
  playername->setMp(playername->getMaxMp());

  myEnemy.setHp(0 - (myEnemy.getHp()));  // 怪物血量先清零
  myEnemy.setHp(myEnemy.getMaxHp());

  playername->manager->Card2DrugList = playername->manager->CardCanUse;
  playername->setHp(playername->getMaxHp());

  // buff重置
  myEnemy.clearbuff();
  playername->clearbuff();

  // 打乱牌组顺序
  srand(unsigned(time(NULL)));
  random_shuffle(playername->manager->Card2DrugList.begin(),
                 playername->manager->Card2DrugList.end());

  // cardNumber 角色一回合可抽几张牌
  playername->manager->Card2DrugList = playername->manager->CardCanUse;
  playername->manager->wastedCard.clear();
  // cardList = game->updataCardList(game->playername->drawNumber);
  for (int i = 0; i < playername->cardNumber; i++) {
    vector<card>::iterator k = playername->manager->Card2DrugList.begin();
    card AnCard = playername->manager->Card2DrugList.front();
    playername->manager->Card2DrugList.erase(k);
    playername->manager->CardInYourHand.push_back(AnCard);
  }

  return playername->manager->CardInYourHand;
}

card WholeProgress::getACard() {
  if (playername->manager->Card2DrugList.empty()) {
    playername->manager->Card2DrugList = playername->manager->wastedCard;
    playername->manager->wastedCard.clear();
    srand(unsigned(time(NULL)));
    random_shuffle(playername->manager->Card2DrugList.begin(),
                   playername->manager->Card2DrugList.end());
  }
  playername->manager->CardInYourHand.push_back(
      playername->manager->Card2DrugList.back());
  playername->manager->Card2DrugList.pop_back();
  return playername->manager->CardInYourHand.back();
}
